﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheOutLetLibrary.Dominio;
using TheOutLetLibrary.Infraestrutura;
using System.Xml;
using System.Xml.Linq;
using Ninject;
using Ninject.Parameters;


namespace TheOutLetUI {

    public partial class FrmCargaProduct : Form {

        private MeuKernel kernel;
        private IFactoryDAO factoryDAO;

        private static FrmCargaProduct frmCargaProduct = null;

        public static FrmCargaProduct Instance() {
            if(frmCargaProduct == null) {
                frmCargaProduct = new FrmCargaProduct();
            }
            return frmCargaProduct;
        }

        private FrmCargaProduct() {
            InitializeComponent();
        }

        private void btnCarregarProducts_Click(object sender, EventArgs e) {
            Cursor.Current = Cursors.WaitCursor;
            DataGridProducts.Rows.Clear();
            labelStatus.Text = "Carregando lista de produtos ...";
            Application.DoEvents();
            Carregadork_product carregadorProduct = new Carregadork_product(factoryDAO);
            IEnumerable<XElement> productsXml = carregadorProduct.CarregueListaDeProducts(Int32.Parse(offset.Text));
            if(productsXml.Count() > 0) {
                decimal qteCarregados = 0;
                foreach(XElement productXml in productsXml) {
                    labelStatus.Text = String.Format("{0:#0%} Carregando produtos ...", qteCarregados / productsXml.Count());
                    Application.DoEvents();
                    k_productLog productLog = carregadorProduct.CarregueProduct(productXml);
                    int row = DataGridProducts.Rows.Add();
                    DataGridProducts.Rows[row].Cells[0].Value = productLog.Product.Id;
                    DataGridProducts.Rows[row].Cells[1].Value = productLog.Product.ShortName;
                    DataGridProducts.Rows[row].Cells[2].Value = productLog.Product.Brand.BrandName;
                    DataGridProducts.Rows[row].Cells[3].Value = productLog.StatusDaCarga;
                    qteCarregados++;
                }
                labelStatus.Text = String.Format("{0:#0%} Concluído", qteCarregados / productsXml.Count());
            } else {
                labelStatus.Text = "Nenhum produto encontrado";
            }
            Cursor.Current = Cursors.Default;
        }

        private void FrmCargaProduct_Load(object sender, EventArgs e) {
            kernel = new MeuKernel();
            factoryDAO = kernel.Get<IFactoryDAO>();
        }


    }
}
